﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;

namespace MvcFlow.Mvc
{
    [Serializable]
    public class HttpRequestData
    {
        private readonly Url                    url;
        private readonly HttpValueDictionary    headerValues;
        private readonly HttpValueDictionary    formValues;
        private readonly HttpValueDictionary    cookieValues;
        private readonly HttpValueDictionary    routeValues;

        public Url                  Url
        {
            get { return url; }
        }

        public HttpValueDictionary  Headers
        {
            get { return headerValues; }
        }

        public HttpValueDictionary  Form
        {
            get { return formValues; }
        }

        public HttpValueDictionary  Cookies
        {
            get { return cookieValues; }
        }

        public HttpValueDictionary  RouteValues
        {
            get { return routeValues; }
        }

        public HttpRequestData(HttpRequestBase request,RouteValueDictionary routeValues)
        {
            this.url            = new Url(request.Url);
            this.headerValues   = new HttpValueDictionary(request.Headers);
            this.formValues     = new HttpValueDictionary(request.Form);
            this.cookieValues   = new HttpValueDictionary(request.Cookies.AllKeys.Select(key => new KeyValuePair<string, string>(key, request.Cookies[key].Value)));

            if ( routeValues != null )
            {
                this.routeValues = new HttpValueDictionary(routeValues.Select(kv => new KeyValuePair<string, string>(kv.Key,kv.Value.ToString())));
            }

            this.routeValues = new HttpValueDictionary();
        }
    }
}